package com.hiyee.api.interceptors;

import com.hiyee.exception.GraceException;
import com.hiyee.grace.result.ResponseStatusEnum;
import com.hiyee.utils.RedisOperator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 基础拦截器类
 *
 * @author WangBin
 */
public class BaseInterceptor {

    // 用户 token
    public static final String REDIS_KEY_USER_TOKEN = "hiyee:user:token:";
    // 用户信息
    public static final String REDIS_KEY_USER_INFO = "hiyee:user:info:";
    // 管理员 token
    public static final String REDIS_KEY_ADMIN_TOKEN = "hiyee:admin:token:";
    // 管理员信息
    public static final String REDIS_KEY_ADMIN_INFO = "hiyee:admin:info:";

    @Autowired
    public RedisOperator redisOperator;

    /**
     * 校验用户 token
     *
     * @param userToken
     * @param userId
     * @param redisKey
     * @return
     */
    public Boolean verifyUserToken(String userToken, String userId, String redisKey)
    {
        if(StringUtils.isBlank(userId) || StringUtils.isBlank(userId)) {
            GraceException.display(ResponseStatusEnum.AUTH_FORBIDDEN);
            return false;
        }

        String redisToken = redisOperator.get(REDIS_KEY_USER_TOKEN + userId);
        if(StringUtils.isBlank(redisToken)) {
            GraceException.display(ResponseStatusEnum.AUTH_FAIL);
            return false;
        }
        if(!userToken.equals(redisToken)) {
            GraceException.display(ResponseStatusEnum.AUTH_FAIL);
            return false;
        }

        return true;

    }
}
